﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;
using ITD_VAPCF.NganQuy.DAL;
using ITD_VAPCF.NganQuy.BLL;

namespace ITD_VAPCF.NganQuy.GUI
{
    public partial class frmTienMat : DevComponents.DotNetBar.Office2007Form
    {
        DataTable dtreport = new DataTable();
        CrystalDecisions.CrystalReports.Engine.ReportClass report;
        private string _mode = "";
        public string MODE
        {
            get { return _mode; }
            set
            {
                _mode = value;
                switch (value)
                {
                    case "BCKKQuyTienMat":
                        lbNgayDL.Text = "Dữ liệu đến ngày: ";
                        lbNgayDL_DN.Visible = false;
                        dtNgayDL_DenNgay.Visible = false;
                        break;
                    default:
                        break;
                }
            }
        }
        string _macn = "";
        public frmTienMat()
        {
            InitializeComponent();
            cboPGD.SetDataSource(ref CommonTable.dtDm_ChiNhanh, "ma", "ten");
            cboPGD.SelectedIndex = 0;
            dtNgayDL_TuNgay.Value = Sys_Infor_SectionWorking.g_WorkingDate;
            dtNgayDL_DenNgay.Value = Sys_Infor_SectionWorking.g_WorkingDate;
        }

        private void chkChiNhanhID_CheckedChanged(object sender, EventArgs e)
        {
            cboPGD.Enabled = chkChiNhanhID.Checked;
        }

        private void btn_In_Click(object sender, EventArgs e)
        {
            switch (MODE)
            {
                case "BaocaoThuChi":
                    BaocaoThuChi();
                    break;
                case "BCKKQuyTienMat":
                    BCKKQuyTienMat();
                    break;
                default:
                    break;
            }

        }
        string MaCN
        {
            get
            {
                if (chkChiNhanhID.Checked)
                {
                    _macn = cboPGD.SelectedValue.ToString();
                }
                else _macn = "";
                return _macn;
            }
        }
        private void BaocaoThuChi()
        {
            dtreport = new DataTable();
            SqlConnection sqlConn = new SqlConnection(CommonVAPCF.GlobalVariables.g_strConnectionMsSql);
            SqlCommand sqlComm = new SqlCommand("sp_NQ_BCThuChiTon", sqlConn);
            sqlComm.CommandType = CommandType.StoredProcedure;
            sqlComm.Parameters.Add("@TuNgay", SqlDbType.VarChar).Value = dtNgayDL_TuNgay.Value.ToString("yyyyMMdd");
            sqlComm.Parameters.Add("@DenNgay", SqlDbType.VarChar).Value = dtNgayDL_DenNgay.Value.ToString("yyyyMMdd");
            sqlComm.Parameters.Add("@ChiNhanhID", SqlDbType.VarChar).Value = MaCN;
            sqlComm.Parameters.Add("@User", SqlDbType.NVarChar).Value = Common.GlobalUser.gs_UserName;
            SqlDataAdapter ad = new SqlDataAdapter(sqlComm);
            ad.Fill(dtreport);
            report = new REPORT.BCThuChiTon();
            report.SetDataSource(dtreport);
            report.SetParameterValue("Ten_Don_vi", Sys_Infor_Company.g_sCompanyName.ToUpper());
            report.SetParameterValue("Dia_chi_Don_vi", Sys_Infor_Company.g_sCompanyAddress);
            report.SetParameterValue("Tieu_De", "BẢNG KÊ LOẠI TIỀN THU CHI TỒN TRONG NGÀY");
            if (dtNgayDL_DenNgay.Value.Date > dtNgayDL_TuNgay.Value.Date)
            {
                StringBuilder subtitle = new StringBuilder("Từ ngày <d1> đến ngày <d2>");
                subtitle.Replace("<d1>", dtNgayDL_TuNgay.Value.ToString("dd/MM/yyyy"));
                subtitle.Replace("<d2>", dtNgayDL_DenNgay.Value.ToString("dd/MM/yyyy"));
                report.SetParameterValue("Ngay_BC", subtitle.ToString());
            }
            else report.SetParameterValue("Ngay_BC", "Ngày " + dtNgayDL_TuNgay.Value.ToString("dd/MM/yyyy"));
            report.SetParameterValue("User_Name", Common.GlobalUser.gs_FullName);
            report.SetParameterValue("PHONG_GD", cboPGD.Text);
            double num = 0;
            for (int i = 0; i < dtreport.Rows.Count; i++)
            {
                num += (CommonVAPCF.ConvertData.ToDouble(dtreport.Rows[i]["TonCuoiKy"]) * CommonVAPCF.ConvertData.ToDouble(dtreport.Rows[i]["MenhGia"]));
            }
            report.SetParameterValue("DOC_SO", CommonVAPCF.Functions.MoneyToString(num.ToString(), true));
            report.SetParameterValue("GIAO_DICH_VIEN", Common.GlobalUser.gs_FullName);
            report.SetParameterValue("THU_QUY", Sys_Infor_Company.g_sCompanyCashier);
            CommonVAPCF.frmPrintPreview frm = new CommonVAPCF.frmPrintPreview();
            frm.LoadDataPrint(report);
            frm.ShowDialog();
        }

        private void BCKKQuyTienMat()
        {
            dtreport = new DataTable();
            SqlConnection sqlConn = new SqlConnection(CommonVAPCF.GlobalVariables.g_strConnectionMsSql);
            SqlCommand sqlComm = new SqlCommand("sp_NQ_BKKQuyTienMat", sqlConn);
            sqlComm.CommandType = CommandType.StoredProcedure;
            sqlComm.Parameters.Add("@ngay", SqlDbType.VarChar).Value = dtNgayDL_TuNgay.Value.ToString("yyyyMMdd");
            sqlComm.Parameters.Add("@macn", SqlDbType.VarChar).Value = MaCN;
            SqlDataAdapter ad = new SqlDataAdapter(sqlComm);
            ad.Fill(dtreport);
            if ((dtreport != null) && (dtreport.Rows.Count > 0))
            {
                double num = CommonVAPCF.ConvertData.ToDouble(dtreport.Rows[0]["SoDuTienMat"]);
                double SO_TON_QUY_DLT = CommonVAPCF.ConvertData.ToDouble(dtreport.Rows[0]["SoTienDLT"]);
                double SO_TON_QUY_KLT = CommonVAPCF.ConvertData.ToDouble(dtreport.Rows[0]["SoTienKLT"]);

                string[] parameterNames = new string[40];
                string[] parameterValues = new string[40];
                parameterNames[0] = "TIEU_DE";
                parameterValues[0] = "B\x00c1O C\x00c1O KIỂM K\x00ca QUỸ TIỀN MẶT";

                parameterNames[1] = "TEN_DON_VI";
                parameterValues[1] = Sys_Infor_Company.g_sCompanyName.ToUpper();
                parameterNames[19] = "DIA_CHI_DON_VI";
                parameterValues[19] = Sys_Infor_Company.g_sCompanyAddress;

                parameterNames[2] = "NGAY_BC";
                parameterValues[2] = "Đến cuối ng\x00e0y " + this.dtNgayDL_TuNgay.Value.ToString("dd") + " th\x00e1ng " + this.dtNgayDL_TuNgay.Value.ToString("MM") + " năm " + this.dtNgayDL_TuNgay.Value.ToString("yyyy");

                parameterNames[3] = "DOC_SO";
                parameterValues[3] = CommonVAPCF.Functions.MoneyToString(SO_TON_QUY_DLT.ToString(), true);

                parameterNames[4] = "SO_TON_QUY_DLT";
                parameterValues[4] = num.ToString("###,###,###,###,###,###") + " VNĐ";
                parameterNames[5] = "DOC_SO_TON_QUY_DLT";
                parameterValues[5] = CommonVAPCF.Functions.MoneyToString(num.ToString(), true);

                parameterNames[6] = "THUA_DLT";
                parameterNames[7] = "THIEU_DLT";
                parameterValues[6] = "Không";
                parameterValues[7] = "Không";
                double THUA_DLT = SO_TON_QUY_DLT - num;
                if (THUA_DLT == 0)
                {
                    parameterValues[6] = "Không";
                    parameterValues[7] = "Không";
                }
                else if (THUA_DLT > 0)
                    parameterValues[6] = THUA_DLT.ToString("###,###,###,###,###") + " VNĐ";
                else
                    parameterValues[7] = Math.Abs(THUA_DLT).ToString("###,###,###,###,###") + " VNĐ";


                parameterNames[8] = "SO_TON_QUY_KLT";
                if (SO_TON_QUY_KLT > 0)
                    parameterValues[8] = SO_TON_QUY_KLT.ToString("###,###,###,###,###,###") + " VNĐ";
                else parameterValues[8] = "0 VNĐ";
                parameterNames[9] = "DOC_SO_TON_QUY_KLT";
                parameterValues[9] = CommonVAPCF.Functions.MoneyToString(SO_TON_QUY_KLT.ToString(), true);

                parameterNames[10] = "THUA_KLT";
                parameterNames[11] = "THIEU_KLT";
                parameterValues[10] = "Kh\x00f4ng";
                parameterValues[11] = "Kh\x00f4ng";

                parameterNames[12] = "NGUYEN_NHAN";
                parameterValues[12] = "";
                parameterNames[13] = "KY_THU_QUY";
                parameterValues[13] = "Thủ quỹ";
                parameterNames[14] = "THU_QUY";
                parameterValues[14] = Sys_Infor_Company.g_sCompanyCashier;
                parameterNames[15] = "KY_KE_TOAN_TRUONG";
                parameterValues[15] = "Kế toán trưởng";
                parameterNames[16] = "KE_TOAN_TRUONG";
                parameterValues[16] = Sys_Infor_Company.g_sCompanyAccountant;
                parameterNames[17] = "KY_GIAM_DOC";
                parameterValues[17] = "Giám đốc";
                parameterNames[18] = "GIAM_DOC";
                parameterValues[18] = Sys_Infor_Company.g_sCompanyDirector;
                parameterNames[20] = "KIEM_SOAT";
                parameterValues[20] = "Kiểm soát";
                parameterNames[21] = "TEN_KIEM_SOAT";
                parameterValues[21] = Sys_Infor_Company.g_sCompanyManager_KiemSoat;
                report = new REPORT.BkkQuyTienMat();
                report.SetDataSource(dtreport);
                for (int i = 0; i < parameterNames.Length; i++)
                {
                    if (parameterNames[i] != null)
                        report.SetParameterValue(parameterNames[i], parameterValues[i]);
                }
                CommonVAPCF.frmPrintPreview frm = new CommonVAPCF.frmPrintPreview();
                frm.LoadDataPrint(report);
                frm.ShowDialog();
            }
            else
            {

            }
            Cursor.Current = Cursors.Default;
        }
        private void btn_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnChotquy_Click(object sender, EventArgs e)
        {
            KKTienMatBLL kkTM = new KKTienMatBLL(GlobalVar.g_strConnectionMsSql);
            bool flag = false;
            int n = (dtNgayDL_DenNgay.Value - dtNgayDL_TuNgay.Value).Days;
            for (int i = 0; i <= n; i++)
            {
                DateTime ngay = dtNgayDL_TuNgay.Value.AddDays(i);
                flag = kkTM.ChotSoLieu(ngay);
            }
            if (flag) Common.Messages.ShowDialog("Chốt số liệu xong!");
        }
    }
}
